package com.ferry.wallpaper.controller;

import com.ferry.wallpaper.service.UserService;
import com.ferry.wallpaper.utils.RandomArtUtils;
import com.ferry.wallpaper.utils.Response;
import com.ferry.wallpaper.utils.ResponseGenerator;
import com.ferry.wallpaper.utils.cg.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Locale;

@Controller
public class LoginController {


    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/code")
    public void code(HttpSession session, HttpServletResponse response) {
        try {
            CodeUtils.Verification code = CodeUtils.getCode(100, 40, 4);
            session.setAttribute("code",code.getText());
            byte[] codeByes = code.getCodeByes();
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(codeByes);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/login")
    public String login(String email, String password, String code,Model model, HttpSession session) {
        String ver = (String) session.getAttribute("code");
        if (code.toLowerCase(Locale.ROOT).equals(ver.toLowerCase(Locale.ROOT))) {
            Response login = userService.login(email, password);
            if (login.getCode() == ResponseGenerator.success) {
                model.addAttribute("user",login.getResponse());
                session.setAttribute("user",login.getResponse());
                session.setAttribute("say",true);
                session.setAttribute("add",false);
                model.addAttribute("sayer", RandomArtUtils.getRandom());
                return "index";
            } else {
                model.addAttribute("msg",login.getMassage());
                return "login";
            }
        } else {
            model.addAttribute("msg","验证码错误");
            return "login";
        }
    }

    @PostMapping("/register")
    public String register(String email,String password,String code,String again,Model model,HttpSession session) {

        String ver = (String) session.getAttribute("code");
        if (code.toLowerCase(Locale.ROOT).equals(ver.toLowerCase(Locale.ROOT))) {
            if (password.equals(again)) {
                Response register = userService.register(email, password);
                if (register.getCode() == ResponseGenerator.success) {
                    model.addAttribute("msg","注册" + email + "成功");
                    return "login";
                } else {
                    model.addAttribute("msg","注册失败");
                    return "register";
                }
            } else {
                model.addAttribute("msg","两次密码不相同");
                return "register";
            }
        } else {
            model.addAttribute("msg","验证码错误");
            return "register";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.setAttribute("user",null);
        return "redirect:/";
    }
}
